<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form :model="where" label-width="77px" class="ele-form-search" @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-row :gutter="15">
          <el-col :lg="6" :md="12">
            <el-form-item label="等级名称:">
              <el-input clearable v-model="where.name" placeholder="请输入等级名称" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button type="primary" icon="el-icon-search" class="ele-btn-icon" @click="reload">查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table ref="table" :where="where" :datasource="url" :columns="columns" :selection.sync="selection"
        height="calc(100vh - 315px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button size="small" type="primary" icon="el-icon-plus" class="ele-btn-icon" @click="openEdit(null)"
            v-if="permission.includes('sys:apply:add')">添加
          </el-button>
          <el-button size="small" type="danger" icon="el-icon-delete" class="ele-btn-icon" @click="removeBatch"
            v-if="permission.includes('sys:apply:dall')">删除
          </el-button>
        </template>
        <template slot="business_code" slot-scope="{row}">
          <el-popover size="mini" trigger="hover">
            <p>企业名称:{{ row.merchant_name }}</p>
            <p>信用代码:{{ row.license_number }}</p>
            <p>法人姓名：{{ row.legal_persona_name }}</p>
            <p>法人微信:{{ row.legal_persona_wechat }}</p>
            <el-tag size="mini" slot="reference">申请信息</el-tag>
          </el-popover>
        </template>
        <template slot="auth_mode" slot-scope="{row}">
          <el-tag size="mini" :type="row.auth_mode | typeFilter">
            {{ row.auth_mode | authModeFilter }}
          </el-tag>
        </template>
        <template slot="apply_status" slot-scope="{row}">
          <el-tag size="mini" :type="row.apply_status | typeFilter">
            {{ row.apply_status | applyStatusFilter }}
          </el-tag>
        </template>
        <template slot="pay_status" slot-scope="{row}">
          <el-tag size="mini" :type="row.pay_status | typeFilter">
            {{ row.pay_status | payStatusFilter }}
          </el-tag>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link type="primary" :underline="false" icon="el-icon-edit" @click="openEdit(row)"
            v-if="permission.includes('sys:apply:edit')">修改
          </el-link>
          <el-popconfirm class="ele-action" title="确定要删除此小程序吗？" @confirm="remove(row)">
            <el-link type="danger" slot="reference" :underline="false" icon="el-icon-delete"
              v-if="permission.includes('sys:apply:delete')">删除
            </el-link>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <apply-edit :data="current" :visible.sync="showEdit" @done="reload" />
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import ApplyEdit from './apply-edit';

export default {
  name: 'Apply',
  components: { ApplyEdit },
  computed: {
    ...mapGetters(["permission"]),
  },
  filters: {
    // 小程序认证类型：10 工商 20 个人
    authModeFilter(auth_mode) {
      const authModeMap = {
        10: '企业',
        20: '个人'
      }
      return authModeMap[auth_mode]
    },
    // <!--支付状态 -->
    payStatusFilter(payStatus) {
      const payStatusMap = {
        10: '待支付',
        20: '已支付',
        30: '免审核'
      }
      return payStatusMap[payStatus]
    },
    // 核状态(10 待审核 20 验证中 30 已审核 40 已驳回)
    applyStatusFilter(apply_status) {
      const applyStatusMap = {
        10: '待审核',
        20: '验证中',
        30: '已审核',
        40: '已驳回'
      }
      return applyStatusMap[apply_status]
    },
    //状态颜色
    typeFilter(status) {
      const typeMap = {
        10: 'success',
        20: 'warning',
        30: 'danger',
        40: 'success',
      }
      return typeMap[status]
    },
  },
  data() {
    return {
      // 表格数据接口
      url: '/apply/index',
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          prop: 'id',
          label: 'ID',
          width: 80,
          align: 'center',
          showOverflowTooltip: true,
          fixed: "left"
        },
        {
          prop: 'app_id',
          label: 'APPID',
          align: 'center',
          showOverflowTooltip: true,
        },
        {
          prop: 'business_code',
          label: '申请编号',
          minWidth: 200,
          align: 'center',
          slot: 'business_code'
        },
        {
          prop: 'auth_mode',
          label: '认证方式',
          align: 'center',
          slot: 'auth_mode',
        },
        {
          prop: 'apply_status',
          label: '审核状态',
          align: 'center',
          slot: 'apply_status',
        },
        {
          prop: 'pay_status',
          label: '支付状态',
          align: 'center',
          slot: 'pay_status',
        },
        {
          prop: 'apply_time',
          label: '审核时间',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          prop: 'create_time',
          label: '创建时间',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          prop: 'update_time',
          label: '更新时间',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 150,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: "right"
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
    };
  },
  methods: {
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({ where: this.where });
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({ lock: true });
      this.$http.post('/apply/delete', { id: row.id }).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的等级吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({ lock: true });
        this.$http.post('/apply/delete', { id: this.selection.map(d => d.id) }).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    }
  }
}
</script>

<style scoped></style>
